package com.sub.dbRouting;

import com.sub.dbRouting.annotation.RouterConstants;

/**
 * @Description: TODO(获取库、表帮助)
 * @author wangkai
 * @date 2018年5月31日
 *
 */
public class DbContextHolder {
	private DbContextHolder() {
	}

	private static DbContextHolder holder;

	public static DbContextHolder init() {
		if (holder == null) {
			synchronized (DbContextHolder.class) {
				if (holder == null) {
					holder = new DbContextHolder();
				}
			}
		}
		return holder;
	}

	/** 数据源key */
	private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
	/** 对应表 */
	private static final ThreadLocal<String> tableIndexHolder = new ThreadLocal<String>();

	public static void setDbKey(String dbKey) {
		contextHolder.set(dbKey);
	}

	public static String getDbKey() {
		if(contextHolder.get()==null){
			return RouterConstants.DATASOURCE_DEFALUT;
		}
		return (String) contextHolder.get();
	}

	public static void clearDbKey() {
		contextHolder.remove();
	}

	public static void setTableIndex(String tableIndex) {
		tableIndexHolder.set(tableIndex);
	}

	public static String getTableIndex() {
		return (String) tableIndexHolder.get();
	}

	public static void clearTableIndex() {
		tableIndexHolder.remove();
	}

}
